package cn.kgc.itrip.controller;


import cn.kgc.itrip.dto.Dto;
import cn.kgc.itrip.service.impl.UserLoginService;
import org.springframework.data.convert.JodaTimeConverters;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/api")
public class UserLoginController {

    @Resource
    private UserLoginService userLOginService;

    /**
     * 用户登录
     * @param name 用户名
     * @param password 登录密码
     * @param request 是否为pc端
     * @return
     */
    @PostMapping(value = "/dologin",consumes = "application/json",produces = "application/json")
    public Dto doLogin(@RequestParam String name,
                       @RequestParam String password,
                       HttpServletRequest request){
        //得到当前用户的客户端是PC还是其他
        String userAgent = request.getHeader("user-agent");
        //传入用户信息进行验证
        Dto dto= null;
        try {
            dto = userLOginService.login(name,password,userAgent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping(value = "/logout",produces = "application/json")
    public Dto logOut(HttpServletRequest request){
        Dto dto=null;
        //从头部去除token(key)和user-agent
        String token = request.getHeader("token");
        String userAgent = request.getHeader("user-agent");
        dto=userLOginService.loOut(token,userAgent);
        return dto;
    }
}
